using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MyProject.Persona
{
//设置面朝方向
    public enum Facing
    {
        Right = 1,
        Left = -1
    }
        
    public struct VirtualJoystick
    {
        public Vector2 Value
        {
            get => new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
        }
    }

    public struct VisualButton
    {
        private KeyCode key; //按下什么按键
        private float bufferTime; //按下按键多长时间触发
        private bool consumed;
        private float bufferCounter;

        public VisualButton(KeyCode key) : this(key, 0)
        {
                
        }

        public VisualButton(KeyCode key, float bufferTime)
        { 
            this.key = key; 
            this.bufferTime = bufferTime;
            consumed = false;
            bufferCounter = 0f;
        }

        //按键缓冲
        public void ConsumeBuffer()
        {
            bufferCounter = 0;
        }

        public bool Press()
        {
            return Input.GetKeyDown(key) || (!consumed && (bufferCounter > 0));
        }

        public bool Checked()
        {
            return Input.GetKey(key);
        }

        public bool Up()
        {
            return Input.GetKeyUp(key);
        }

        public void Update(float deltaTime)
        {
            consumed = false;
            bufferCounter -= Time.deltaTime;
            bool flag = false;
            if (Input.GetKeyDown(key))
            { 
                flag = true;
                bufferCounter = bufferTime;
            }
            else if (Input.GetKey(key))
            {
                flag = true;
            }

            if (!flag)
            {
                bufferCounter = 0f;
            }
        }
    }

    public static class GameInput
    {
        public static VisualButton jump = new VisualButton(KeyCode.Space, 0f);
        // public static VisualButton Dash = new VisualButton(KeyCode.E, 0.08f);
        public static VirtualJoystick aim = new VirtualJoystick();
        public static Vector2 lastAim;

        public static Vector2 GetAimVector(Facing face = Facing.Right)
        {
            Vector2 value = aim.Value;

            if (value == Vector2.zero)
            {
                lastAim = Vector2.right * ((int)face);
            }
            else
            {
                lastAim = value;
            }

            return lastAim.normalized;
        }

        public static void Update(float deltaTime)
        {
            jump.Update(deltaTime);
        }
    }
}

